<?php

namespace app\common;

use support\Request; //请求类
use support\Response; //请求类
use think\facade\Db; //数据库类
use app\common\traits\JumpTrait;

class DnfmailController
{
    /*账号取UID,传账号,没有返回-1*/
    public function getuid($name){
        $uinfo = Db::connect('d_taiwan')->table('accounts')->where(['accountname'=>$name])->find();
        $return = '-1';
        if($uinfo){
            $return = $uinfo['UID'];
        }
        return $return;
    }
    
    /*UID取角色列表*/
    public function getnolist($uid){
        $sqlconnect = DB::connect('taiwan_cain');
        $sqlconnect->execute('set names latin1');
        $list = $sqlconnect->table('charac_info')->where('m_id',$uid)->where('delete_flag','<>','1')->field('m_id, charac_name, charac_no')->select();
        return $list;
    }
    
    /*发送邮件
    角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
    public function senddnfmail($noid,$wupid,$num,$hztype,$hznum,$gold,$fz,$dz,$qh,$info,$title,$account,$gamename,$bf=0){
        $return = '1';
        try {
            $taiwan_cain_2nd = Db::connect('taiwan_cain_2nd');
            $taiwan_cain_2nd->execute('set names latin1');
            $taiwan_cain_2nd->startTrans();
            $inserted_id = $taiwan_cain_2nd->table('letter')->insertGetId(['reg_date' => date('Y-m-d-H-i-s'),'charac_no' => $noid,'send_charac_no'=>'0','send_charac_name'=>$title,'letter_text'=>$info,'stat'=>'1']);
            
            $taiwan_cain_2nd->table('postal')->insert(
                [   'occ_time' => date('Y-m-d-H-i-s'),
                    'receive_charac_no' => $noid,
                    'send_charac_no'=>'0',
                    'send_charac_name'=> $title,
                    'item_id'=>$wupid,
                    'add_info'=>$num,
                    'upgrade'=>$qh,
                    'amplify_option'=>$hztype,
                    'amplify_value'=>$hznum,
                    'gold'=>$gold,
                    'seal_flag'=>$fz,
                    'letter_id'=>$inserted_id,
                    'seperate_upgrade'=>$dz
                ]);
            $taiwan_cain_2nd->commit();
            } catch (\Exception $e) {
                //var_dump('失败'.$e->getMessage());
                $taiwan_cain_2nd->rollback();
                $return = "0";
            }
        //插入邮件记录
        if($bf == 0){
            DB::name('send_log')->insert(['noid'=>$noid,'wupid'=>$wupid,'num'=>$num,'hztype'=>$hztype,'hznum'=>$hznum,'gold'=>$gold,'fz'=>$fz,'dz'=>$dz,'qh'=>$qh,'info'=>$info,'addtime'=>time(),'status'=>$return,'title'=>$title,'account'=>$account,'gamename'=>$gamename]);
        }else{
            if($return != "0"){}
            DB::name('send_log')->where('id',$bf)->update(['addtime'=>time(),'status'=>'2']);
        }
        return $return;
    }
}